package com.hxd.otc.module.my.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.adapter.ExchangeRecordAdapter;
import com.hxd.otc.module.my.bean.ExchangeRecordBean;
import com.hxd.otc.module.my.contract.ExchangeRecoderContract;
import com.hxd.otc.module.my.presenter.ExchangeRecordPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;

public class ExchangeRecoderActivity extends BaseActivity<ExchangeRecordPresenter> implements ExchangeRecoderContract.View {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    ExchangeRecordAdapter recoderAdapter;

    private boolean isRefresh = true;
    private int currentPage = 1;//当前页
    private int rows = 15;//每页显示条数

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exchange_recoder;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;
                currentPage = currentPage + 1;
                mPresenter.doLoadData(rows + "", currentPage + "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mPresenter.doLoadData(rows + "", currentPage + "");

            }
        });
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });
    }

    @Override
    protected void initData() {
        initAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    private void initAdapter() {
        if (recoderAdapter == null) {
            recoderAdapter = new ExchangeRecordAdapter();
            recoderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(ExchangeDetailActivity.EXTRA_EXCHANGE_RECORD, (ExchangeRecordBean) adapter.getItem(position));
                    $startActivity(ExchangeDetailActivity.class, bundle);
                }
            });
        }
        recyclerView.setAdapter(recoderAdapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected ExchangeRecordPresenter createPresenter() {
        return new ExchangeRecordPresenter(this);
    }

    @Override
    public void onShowNoMore() {

        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void onSetAdapter(ArrayList<ExchangeRecordBean> list) {
        if (isRefresh) {
            mRefreshLayout.setEnableLoadMore(true);
            if (list == null || list.size() == 0) {
                recoderAdapter.setEmptyView(notDataView);
            }
            recoderAdapter.setNewData(list);
        } else {
            if (list == null || list.size() < rows) {
                mRefreshLayout.setEnableLoadMore(false);
            } else {
                mRefreshLayout.setEnableLoadMore(true);
                recoderAdapter.addData(list);
            }
        }
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        if (isRefresh)
            recoderAdapter.setEmptyView(errorView);
    }
}
